﻿using BBICMS.Application.ViewModels;
using BBICMS.Model.Security;
using BBICMS.Repository;
using System;
using System.Linq;
using System.Threading;
using System.Web.Mvc;

namespace BBICMS.WebUI.Controllers
{
    public class BaseController : Controller
    {
        public AjaxResultVM AR = new AjaxResultVM();
        public ApplicationUnit _unit = new ApplicationUnit();
        protected virtual new CustomPrincipal User
        {
            get { return HttpContext.User as CustomPrincipal; }
        }

        protected string GetModelErrorMessage()
        {
            string validationErrors = string.Join("|",
                    ModelState.Values.Where(E => E.Errors.Count > 0)
                    .SelectMany(E => E.Errors)
                    .Select(E => E.ErrorMessage)
                    .ToArray());
            return validationErrors;
        }

        protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
        {
            //var seoName = RouteData.Values["seoName"] as string;
            //if (seoName != null)
            //{
            //    switch (seoName.ToLower())
            //    {
            //        case "contact":
            //            RouteData.Values["seoName"] = null;
            //            RouteData.Values["controller"] = "Contact";
            //            RouteData.Values["action"] = "Index";
            //            Response.RedirectToRoute(RouteData.Values);
            //            break;
            //        case "products":
            //            RouteData.Values["seoName"] = null;
            //            RouteData.Values["controller"] = "Products";
            //            RouteData.Values["action"] = "Index";
               
            //            Response.RedirectToRoute(RouteData.Values);
                       
            //            break;
            //    }               
                
            //}
            string cultureName = RouteData.Values["culture"] as string;
           
            // Attempt to read the culture cookie from Request
            if (cultureName == null)
                cultureName = Request.UserLanguages != null && Request.UserLanguages.Length > 0 ? Request.UserLanguages[0] : null; // obtain it from HTTP header AcceptLanguages

            var url = cultureName;
            // Validate culture name
            cultureName = LanguageHelper.GetImplementedCulture(cultureName); // This is safe
            if (RouteData.Values["culture"] as string != cultureName)
            {
                // Force a valid culture in the URL
                RouteData.Values["culture"] = cultureName.ToLowerInvariant(); // lower case too
                                                                              // Redirect user
                                                                              //  Response.RedirectPermanent(cultureName + "/" + url);
                RouteData.DataTokens["area"] = "";
                Response.RedirectToRoute(RouteData.Values);
            }

            // Modify current thread's cultures            
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

            return base.BeginExecuteCore(callback, state);

        }
    }
}